# Maintainer: Martell Malone <martellmalone@gmail.com>

MINGW_PACKAGE_PREFIX=mingw-w64-cross

_realname=gcc
_base_ver=5.0.0
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=5.0.0.133436.b1d4d30
pkgrel=1
pkgdesc="GCC toolchain for MinGW-w64"
arch=('i686' 'x86_64')
url="https://gcc.gnu.org"
license=('GPL' 'LGPL' 'FDL' 'custom')
groups=("${MINGW_PACKAGE_PREFIX}-toolchain" "${MINGW_PACKAGE_PREFIX}")

depends=("zlib"
#         "${MINGW_PACKAGE_PREFIX}-crt"
#         "${MINGW_PACKAGE_PREFIX}-winpthreads"
         "${MINGW_PACKAGE_PREFIX}-binutils"
         "${MINGW_PACKAGE_PREFIX}-headers")
makedepends=('gcc' 'git' 'lndir' 'gmp-devel' 'mpfr-devel' 'mpc-devel' 'cloog-devel' 'isl-devel' )

options=('!strip' 'staticlibs' '!emptydirs' '!buildflags')
source=("${_realname}"::"git+https://github.com/gcc-mirror/gcc.git#branch=master"
    'gcc-4.8-libstdc++export.patch'
    'gcc-4.7-stdthreads.patch'
    '4.7-execstack.patch'
    '5.0-libitm-cygwin.patch'
    '5.0-lto-plugin-soname.patch'
    'gcc-5.0-configure-msys2.patch'
    'gcc-4.8.0-msys2-spec.patch'
    'armv7-w32-mingw32ce.patch')

sha256sums=('SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP')

_threads="win32"

#_targets="i686-w64-mingw32 x86_64-w64-mingw32 armv7-w64-mingw32ce"
_targets="armv7-w64-mingw32ce"

pkgver() {
  cd "$srcdir/$_realname"
  printf "%s.%s.%s" "${_base_ver}" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd ${srcdir}/${_realname}

  #do not install libiberty
  sed -i 's/install_to_$(INSTALL_DEST) //' libiberty/Makefile.in
  # hack! - some configure tests for header files using "$CPP $CPPFLAGS"
  #sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" {libiberty,gcc}/configure

  # mingw-w64 Patches
  patch -p1 -i ${srcdir}/gcc-4.8-libstdc++export.patch
  patch -p1 -i ${srcdir}/gcc-4.7-stdthreads.patch

  #MSYS/Cygwin patches
  patch -p2 -i ${srcdir}/4.7-execstack.patch
  patch -p2 -i ${srcdir}/5.0-libitm-cygwin.patch
  patch -p0 -i ${srcdir}/5.0-lto-plugin-soname.patch

  #now on head after 4.9 release
  #patch -p2 -i ${srcdir}/4.9-libffi-cygwin64.patch
  #patch -p0 -i ${srcdir}/4.9-specs-cygwin.patch
  #now on head as of 4.8.3+ ?
  #patch -p2 -i ${srcdir}/4.7-libstdc-cygwin.patch

  #msys2 patches
  patch -p1 -i ${srcdir}/gcc-5.0-configure-msys2.patch
  patch -p1 -i ${srcdir}/gcc-4.8.0-msys2-spec.patch

  #armv7 patches
  patch -p0 -i ${srcdir}/armv7-w32-mingw32ce.patch
}

build() {
  for _target in ${_targets}; do
  mkdir -p ${srcdir}/gcc-build-${_target} && cd ${srcdir}/gcc-build-${_target}

  case "${_target}" in
    i686*)
      local _conf="--disable-sjlj-exceptions --with-dwarf2"
      local _arch=i686
    ;;

    x86_64*)
      local _arch=x86-64
      local _conf=""
    ;;

    armv7*)
      local _arch=armv7
      local _conf=""
    ;;
  esac

  ../${_realname}/configure \
    --prefix=/opt \
    --build=${CHOST} \
    --host=${CHOST} \
    --target=${_target} \
    --libexecdir=/opt/lib \
    --disable-bootstrap \
    --with-arch=${_arch} \
    --with-tune=generic \
    --enable-languages=c,lto,c++ \
    --enable-shared --enable-static \
    --enable-threads=${_threads} \
    --enable-graphite \
    --enable-fully-dynamic-string \
    --enable-libstdcxx-time=yes \
    --disable-libstdcxx-pch \
    --disable-libstdcxx-debug \
    --enable-cloog-backend=isl \
    --enable-version-specific-runtime-libs \
    --disable-cloog-version-check --disable-isl-version-check \
    --enable-lto \
    --enable-libgomp \
    --disable-multilib \
    --enable-checking=release \
    --disable-rpath \
    --disable-win32-registry \
    --disable-werror \
    --disable-symvers \
    --with-libiconv \
    --with-system-zlib \
    --with-{gmp,mpfr,mpc,isl,cloog}=/usr \
    --with-gnu-as --with-gnu-ld \
    ${_conf} \
    CFLAGS="-O2 -pipe" \
    CXXFALGS="-O2 -pipe"

  make all gcc
  #make all
  done
}

package() {
  for _target in ${_targets}; do
    cd ${srcdir}/gcc-build-${_target}
    #make -j1 DESTDIR=${pkgdir} install
    make -j1 DESTDIR=${pkgdir} install gcc
    mv ${pkgdir}/opt/lib/gcc/${_target}/lib/libgcc_s.a ${pkgdir}/opt/lib/gcc/${_target}/${pkgver}/
    mv ${pkgdir}/opt/lib/gcc/${_target}/libgcc*.dll ${pkgdir}/opt/lib/gcc/${_target}/${pkgver}/
    for _f in ${pkgdir}/opt/bin/*.exe; do
      strip -s ${_f}
    done
    for _f in ${pkgdir}/opt/lib/gcc/${_target}/${pkgver}/*.exe; do
      strip -s ${_f}
    done
    rm -rf ${pkgdir}/opt/share
  done
}
